'''
Created on Mar 26, 2013

@author: Petko
'''
import unittest
from utils.sententialyzer_client import SententialyzerClient
import json
from opinion import checkAttentions, checkSubopStats


class UpdateOpinionTest(unittest.TestCase):

    sc = SententialyzerClient()
    
    def setUp(self):
        pass
    
    def tearDown(self):
        pass

    def test_create_from_same_user(self):
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        baseOp = self.sc.createBaseOp("attentions test base", "bla bla bla", "moderators:d sent_default_group:rc")
        baseOpId = baseOp['id']
        
        op1 = self.sc.authAndCreateOp(baseOpId, baseOpId, 'positive', title = 'op1+', content = 'op plus')
        
        self.sc.authorize('updating_commenter', 'emptypass', baseOpId + ':rc')
        op11 = self.sc.createOp(op1['id'], 'positive', title = 'op11', content = 'op')
        
        self.sc.authAndCreateOp(baseOpId, op11['id'], 'positive', title = 'op111', content = 'op plus')
        
        checkAttentions(self, baseOpId, 3, 0)
        checkAttentions(self,op1['id'], 2, 0)
        checkAttentions(self,op11['id'], 1, 0)
        
        checkSubopStats(self, baseOpId, 1, 0, 0, 1)
        
        self.sc.authorize('updating_commenter', 'emptypass', baseOpId + ':rc')
        self.sc.updateOp(op11['id'], evaluation = 'negative', content='new content')
        
        checkAttentions(self,baseOpId, 1, 2)
        checkAttentions(self,op1['id'], 0, 2)
        checkAttentions(self,op11['id'], 1, 0)
        
        checkSubopStats(self, baseOpId, 1, 0, 0, 1)
        checkSubopStats(self, op1['id'], 0, 1, 0, 1)
        
        op = self.sc.getOpSimple(op11['id'], 'most_positive', max_content_length=12, max_depth=0)
        self.assertEqual('new content', op['text'])
    
    def test_delete_opinion(self):
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        baseOp = self.sc.createBaseOp("testDeleteOpinion", "bla bla bla", "moderators:d sent_default_group:rc")
        
        baseOpId = baseOp['id']
        
        op1 = self.sc.authAndCreateOp(baseOpId, baseOpId, 'positive', title = 'op1+')
        
        op11 = self.sc.authAndCreateOp(baseOpId, op1['id'], 'positive', title='x')
        
        checkAttentions(self,baseOpId, total_content = 2)
        checkAttentions(self,op1['id'], total_content = 1)
        
        res = self.sc.deleteOp(op11['id'])
        print json.dumps(res)
        
        checkAttentions(self,baseOpId, total_content = 1)
        checkAttentions(self,op1['id'], total_content = 0)
        
        checkSubopStats(self, baseOpId, 1, 0, 0, 1)
        checkSubopStats(self, op1['id'], 0, 0, 0, 0)
        
    def test_like_my_own_comment(self):
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        baseOp = self.sc.createBaseOp("some title", "some content", "moderators:d sent_default_group:rc")
        baseOpId = baseOp['id']
        
        #make a comment
        op1 = self.sc.authAndCreateOp(baseOpId, baseOpId, 'positive', title = 'op1', content = 'op')
        access_token_backup = self.sc.accessToken
        
        #someone likes your comment
        self.sc.authAndCreateOp(baseOpId, op1['id'], 'positive')
        
        checkAttentions(self,baseOpId, 2, 0, 1)
        checkAttentions(self,op1['id'], 1, 0, 0)
        
        checkSubopStats(self, op1['id'], 1, 0, 0, 0)
        
        #but you are dummy and like your own comment
        self.sc.accessToken = access_token_backup
        self.sc.createOp(op1['id'], 'positive')
        
        #you previous opinion becomes neutral now and does not score for the base opinion
        checkAttentions(self,baseOpId, 0, 0, 1)
        checkAttentions(self,op1['id'], 2, 0, 0)
        
        checkSubopStats(self, baseOpId, 0, 0, 1, 1)
        checkSubopStats(self, op1['id'], 2, 0, 0, 0)
        
        #now you are crazy and dislike your own comment (WTF)
        self.sc.accessToken = access_token_backup
        self.sc.createOp(op1['id'], 'negative')
        
        checkAttentions(self,baseOpId, 0, 0, 1)
        checkAttentions(self,op1['id'], 1, 1, 0)
        
        checkSubopStats(self, baseOpId, 0, 0, 1, 1)
        checkSubopStats(self, op1['id'], 1, 1, 0, 0)
        
        #recover your support of the base opinion 
        self.sc.updateOp(op1['id'], evaluation="positive")
        
        #now your like should be deleted (check the total opinions)
        checkAttentions(self,baseOpId, 2, 0, 1)
        checkAttentions(self,op1['id'], 1, 0, 0)
        
        checkSubopStats(self, op1['id'], 1, 0, 0, 0)
        
    def test_like_my_own_negative_comment(self):
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        baseOp = self.sc.createBaseOp("some title", "some content", "moderators:d sent_default_group:rc")
        baseOpId = baseOp['id']
        
        #make a comment
        op1 = self.sc.authAndCreateOp(baseOpId, baseOpId, 'negative', title = 'op1', content = 'op')
        accessTokenBackup = self.sc.accessToken
        
        #someone likes your comment
        self.sc.authAndCreateOp(baseOpId, op1['id'], 'positive')
        
        checkAttentions(self,baseOpId, 0, 2, 1)
        checkAttentions(self,op1['id'], 1, 0, 0)
        
        #but you are dummy and like your own comment
        self.sc.accessToken = accessTokenBackup
        self.sc.createOp(op1['id'], 'positive')
        
        #you previous opinion becomes neutral now and does not score for the base opinion
        checkAttentions(self,baseOpId, 0, 0, 1)
        checkAttentions(self,op1['id'], 2, 0, 0)
        
        #now you are crazy and dislike your own comment (WTF)
        self.sc.accessToken = accessTokenBackup
        self.sc.createOp(op1['id'], 'negative')
        
        checkAttentions(self,baseOpId, 0, 0, 1)
        checkAttentions(self,op1['id'], 1, 1, 0)
        
        #recover your dislike of the base opinion 
        self.sc.updateOp(op1['id'], evaluation="negative")
        
        #now your like should be deleted (check the total opinions)
        checkAttentions(self,baseOpId, 0, 2, 1)
        checkAttentions(self,op1['id'], 1, 0, 0)
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()